package com.lc.config;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.lc.bean.User;
import org.springframework.context.annotation.Configuration;

import java.util.Date;
@Configuration
public class JWTUntil {
    //加的盐
    private static final String KEY="ydy";
    //签发人
    private static final String Issuer="zdqz";
    //失效时间
    private static final Long TOKEN_EXIRE_TIME=20*1000*60L;
    public String token(User user){
        Date date=new Date();
        //
        Algorithm algorithm=Algorithm.HMAC256(KEY);

        String token= JWT.create()
                .withIssuer(Issuer)  //签发人
                .withIssuedAt(date)  //签发时间
                .withExpiresAt(new Date(date.getTime()+TOKEN_EXIRE_TIME)) //失效时间
                .withClaim("username",user.getName())//加密username,也可以加密多个,但是要唯一的
                .sign(algorithm);//
        return token;
    }

    public String verify(String token,String username){
        try {
            //反解密
            Algorithm algorithm=Algorithm.HMAC256(KEY);
           JWTVerifier jwtVerifier=JWT.require(algorithm).withIssuer(Issuer).withClaim("username",username).build();
            jwtVerifier.verify(token);
            return "success";
        }catch (Exception ex){

        }
        return "success";

    }

}
